/**
 * 
 * 	Bitmap	类的扩展，增加了对于位图的九宫格缩放的控制
 * 
 **/
 
package org.dstart.draw.csuper
{
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.geom.Matrix;
	import flash.geom.Rectangle;

	public class CBitmap extends Bitmap
	{
		private var m_bmp:BitmapData;	//	BitmapData 对象，用于缓存传入的位图
		private var m_rect:Rectangle;	//	scale9Grid 的范围，初始无值
		
		public function CBitmap(bitmapData:BitmapData = null, pixelSnapping:String = "auto", smoothing:Boolean = false)
		{
			super(bitmapData, pixelSnapping, smoothing);
			
			if(bitmapData)
			{
				m_bmp = bitmapData.clone();
			}
		}
		
		//	重写读取位图方法（读）
		public override function get bitmapData():BitmapData
		{			
			return m_bmp;
		}
		
		//	重写设置位图方法（写）
		public override function set bitmapData(value:BitmapData):void
		{
			m_bmp = value.clone();
		}
		
		//	重写 width 属性（写）
		public override function set width(value:Number):void		
		{
			if (value != width) {
				setSize(value, height);
			}
		}

		//	重写 height 属性（写）
		public override function set height(value:Number):void 
		{
			if (value != height) {
				setSize(width, value);
			}
		}
		
		//	重写 scale9Grid 属性（写）
		public override function set scale9Grid(r:Rectangle):void
		{
			if( (m_rect == null && r != null) || (m_rect != null && !m_rect.equals(r)) ) 
			{
				if (r == null) 
				{
					var currentWidth : Number = width;
					var currentHeight : Number = height;
					m_rect = null;					
					setSize(currentWidth, currentHeight);
				}
				else 
				{
					if (!validGrid(r)) 
					{
						throw new Error("传入的范围不正确！！");
						return;
					}
					
					m_rect = r.clone();
					resizeBitmap(width, height);
					scaleX = 1;
					scaleY = 1;
				}
			}
		}

		//	重写 scale9Grld 属性（读）
		public override function get scale9Grid():Rectangle
		{
			return m_rect;
		}
		
		//--------------------------------------------------------------------------------
		
		public function setSize(w:Number, h:Number):void 
		{
			if(m_rect == null) 
			{
				super.width = w;
				super.height = h;
			}
			else
			{
				w = Math.max(w, m_bmp.width - m_rect.width);
				h = Math.max(h, m_bmp.height - m_rect.height);
				resizeBitmap(w, h);
			}
		}

		//--------------------------------------------------------------------------------

		//	判断是否为合法的九宫格参数
		private function validGrid(r : Rectangle) : Boolean 
		{
			if(m_bmp)
			{
				return r.right <= m_bmp.width && r.bottom <= m_bmp.height;
			}
			return false;
		}
		
		//--------------------------------------------------------------------------------
		
		//	修改
		protected function resizeBitmap(w : Number, h : Number) : void {
			
			var bmpData:BitmapData = new BitmapData(w, h, true, 0x00000000);
			
			var rows:Array = [0, m_rect.top, m_rect.bottom, m_bmp.height];
			var cols:Array = [0, m_rect.left, m_rect.right, m_bmp.width];
			
			var dRows:Array = [0, m_rect.top, h - (m_bmp.height - m_rect.bottom), h];
			var dCols:Array = [0, m_rect.left, w - (m_bmp.width - m_rect.right), w];

			var origin:Rectangle;
			var draw:Rectangle;
			var mat:Matrix = new Matrix();
			
			for (var cx : int = 0;cx < 3; cx++) {
				for (var cy : int = 0 ;cy < 3; cy++) {
					origin = new Rectangle(cols[cx], rows[cy], cols[cx + 1] - cols[cx], rows[cy + 1] - rows[cy]);
					draw = new Rectangle(dCols[cx], dRows[cy], dCols[cx + 1] - dCols[cx], dRows[cy + 1] - dRows[cy]);
					mat.identity();
					mat.a = draw.width / origin.width;
					mat.d = draw.height / origin.height;
					mat.tx = draw.x - origin.x * mat.a;
					mat.ty = draw.y - origin.y * mat.d;
					bmpData.draw(m_bmp, mat, null, null, draw, smoothing);
				}
			}
			
			bitmapData = bmpData;
		}

	}
}